package com.wyp168.leetcode;

/**
 * @ClassName IsPrefixOfWord
 * @Description TODO  检查一个英文句子中是否有以指定单词为前缀的单词，如果有，则返回句子中的单词下标，如果没有，则返回-1
 * @Author wyp168
 * @Date 2022/8/21 16:26
 */
public class IsPrefixOfWord {
    public static void main(String[] args) {
        IsPrefixOfWord isPrefixOfWord = new IsPrefixOfWord();
        String sentence = "i love eating burger";
        String searchWord = "burg";
        int prefixOfWord = isPrefixOfWord.isPrefixOfWord(sentence, searchWord);
        System.out.println(prefixOfWord);
    }

    public int isPrefixOfWord(String sentence, String searchWord) {
        String[] s = sentence.split(" ");
        for (int i = 0; i < s.length; i++) {
            if (s[i].indexOf(searchWord) == 0) {
                return i + 1;
            }
        }
        return -1;
    }
}
